#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
样本管理桌面应用程序
基于TCP与串口通信的实验室样本管理系统
"""

import sys
import os
import json
import sqlite3
import logging
from datetime import datetime
from typing import Dict, List, Optional, Any
from dataclasses import dataclass, asdict
from enum import Enum

from PyQt6.QtWidgets import (
    QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
    QTabWidget, QTableWidget, QTableWidgetItem, QPushButton,
    QLabel, QLineEdit, QComboBox, QSpinBox, QTextEdit, QMessageBox,
    QFileDialog, QDialog, QFormLayout, QGroupBox, QCheckBox,
    QDateEdit, QTimeEdit, QSplitter, QFrame, QHeaderView,
    QAbstractItemView, QMenu, QStatusBar, QProgressBar
)
from PyQt6.QtCore import (
    Qt, QThread, pyqtSignal, QTimer, QDate, QTime, QSize,
    QPropertyAnimation, QEasingCurve
)
from PyQt6.QtGui import (
    QFont, QPixmap, QIcon, QPalette, QColor, QPainter,
    QBrush, QPen, QActionEvent, QAction
)

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('sample_management.log', encoding='utf-8'),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)

class UserRole(Enum):
    """用户角色枚举"""
    ADMIN = "管理员"
    OPERATOR = "操作员"
    VIEWER = "查看员"

class SampleStatus(Enum):
    """样本状态枚举"""
    PENDING = "待处理"
    PROCESSED = "已处理"
    EXPORTED = "已导出"
    SENT = "已发送"

class CommunicationType(Enum):
    """通信类型枚举"""
    TCP = "TCP/IP"
    SERIAL = "串口"

class ProtocolType(Enum):
    """协议类型枚举"""
    CUSTOM = "自有协议"
    HL7 = "HL7协议"

@dataclass
class User:
    """用户数据类"""
    id: int
    username: str
    password_hash: str
    role: UserRole
    created_time: datetime
    last_login: Optional[datetime] = None

@dataclass
class Sample:
    """样本数据类"""
    id: str
    instrument_id: str
    instrument_model: str
    instrument_number: str
    protocol_type: str
    communication_config: str
    sample_number: str
    sample_id: str
    instrument_type: str
    analysis_time: datetime
    patient_name: str = ""
    patient_age: int = 0
    patient_gender: str = ""
    test_image_path: str = ""
    test_results: str = ""
    report_id: str = ""
    status: SampleStatus = SampleStatus.PENDING
    created_time: datetime = None
    updated_time: datetime = None

@dataclass
class OperationLog:
    """操作日志数据类"""
    id: int
    operation_time: datetime
    operator: str
    operation_type: str
    operation_content: str
    ip_address: str = ""

def main():
    """主函数"""
    app = QApplication(sys.argv)
    
    # 设置应用程序信息
    app.setApplicationName("样本管理系统")
    app.setApplicationVersion("1.0.0")
    app.setOrganizationName("实验室管理系统")
    
    # 创建主窗口
    from main_window import SampleManagementSystem
    window = SampleManagementSystem()
    window.show()
    
    # 运行应用程序
    sys.exit(app.exec())

if __name__ == "__main__":
    main() 